<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 0"
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:information:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 0"
          type="success"
          plain
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:information:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 0"
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:information:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 0"
          type="success"
          plain
          size="mini"
          @click="importStudentInfo"
          v-hasPermi="['student:information:remove']"
        >导入学生信息</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 1"
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="uploadClassInfo"
        >上传班级附件</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
        v-if="isEnd == 1"
          type="success"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="confirmTrainisPass"
        >是否已经上传完所有的信息</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="工作单位" align="center" prop="workplace" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="个人申请表(附件)" align="center" prop="urlPersonApplication" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlPersonApplication" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="健康承诺书(附件)" align="center" prop="urlHealthCertificate" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlHealthCertificate" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="学历证明(附件)" align="center" prop="urlDegreeCertificate" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlDegreeCertificate" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证复印件(附件)" align="center" prop="urlIdCopy" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.urlIdCopy" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button
          v-if="isEnd == 1" 
          size="mini"
          style="width: 100px;"
          type="success" 
          @click="handleSubmit(scope.row)">
          输入培训结论</el-button>      

          <el-button
          v-if="isEnd == 0"
            size="mini"
            style="width: 50px;"
            type="warning"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:information:edit']"
          >修改</el-button>
          <el-button
          v-if="isEnd == 0"
            size="mini"
            type="danger"
            style="width: 50px; margin-top: 10px;"
            @click="handleDelete(scope.row)"
            v-hasPermi="['student:information:remove']"
          >删除</el-button>

          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="140px" v-if="isEnd == 0">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input  v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="工作单位" prop="workplace">
          <el-input v-model="form.workplace" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="个人申请表(附件)" prop="urlPersonApplication" >
          <image-upload v-model="form.urlPersonApplication"/>
        </el-form-item>
        <el-form-item label="健康承诺书(附件)" prop="urlHealthCertificate">
          <image-upload v-model="form.urlHealthCertificate"/>
        </el-form-item>
        <el-form-item label="学历证明(附件)" prop="urlDegreeCertificate">
          <image-upload v-model="form.urlDegreeCertificate"/>
        </el-form-item>
        <el-form-item  label="身份证复印件(附件)" prop="urlIdCopy">
          <image-upload v-model="form.urlIdCopy"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 录入考生结论 -->
    <el-dialog title="录入考生结论" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form :inline="true" ref="form" :model="form" label-width="100px" v-if="isEnd == 1" >
        <el-form-item label="学生培训结论" prop="trainConclusion" >
          <el-input type="textarea" v-model="form.trainConclusion" placeholder="请输入培训结论" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitExamConclusion">确 定</el-button>
      </span>
</el-dialog>

<!-- 上传班级附件 -->
    <el-dialog title="上传班级附件" :visible.sync="dialogVisibleClass" width="1100px" :before-close="handleClose" >
          <el-form :inline="true" ref="formClass" :model="formClass" label-width="170px" v-if="isEnd == 1" >
            <el-form-item  label="签到表(附件)" prop="urlSign">
          <image-upload v-model="formClass.urlSign"/>
          </el-form-item>

          <el-form-item  label="培训评估报告(附件)" prop="urlInvalue">
            <image-upload v-model="formClass.urlInvalue"/>
          </el-form-item>

          <el-form-item  label="安全培训合格证明(附件)" prop="urlCertificate">
            <image-upload v-model="formClass.urlCertificate"/>
          </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisibleClass = false">取 消</el-button>
            <el-button type="primary" @click="submitUploadClass">确定</el-button>
          </span>
    </el-dialog>


    <!-- 导入学生信息 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
        <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?classId=' + classId + '&trainId=' + trainId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
        >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:20px; color: green;" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import { getUrlParam } from "../../../api/utils";
import { listInformation, getInformation, delInformation, addInformation, updateInformation, submitConclusion, getInformationClass, submitClassInfo, confirmTrainisPassAll } from "@/api/student/information";
import { importTemplate } from "@/api/student/information";
import { getToken } from "@/utils/auth";

export default {
  name: "Information",
  data() {
    return {
      trainId: "",
      // 用户导入参数
      upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/student/information/importData"
        },
      // 班级附件上传弹框
      dialogVisibleClass: false,
      // 班级附件表
      formClass: [],
      // 培训结论
      trainClusion: "",
      // 培训结论弹框
      dialogVisible: false,
      // 培训结束标志
      isEnd: "",
      stuId: "",
      // 班级id
      classId: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.classId = getUrlParam("classId");
    this.isEnd = getUrlParam("isEnd");
    this.trainId = getUrlParam("trainId");
    this.getList();
    
  },
  methods: {
    // 导入学生信息
    importStudentInfo() {
      this.upload.title = "学生基本信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/student/information/importTemplate', {
      }, `student_template_${new Date().getTime()}.xlsx`)
    },
    // 是否已经上传完所有信息
    confirmTrainisPass() {
      const ids = this.classId;
      const trainId = getUrlParam("trainId")
      this.$modal.confirm('是否确定通过培训，加入考试合格？').then(function() {
        return confirmTrainisPassAll(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("通过成功");
        window.location.href = "../class/class?trainId=" + trainId;
      }).catch(() => {});
    },
    // 提交班级附件
    submitUploadClass() {
      this.formClass.classId = this.classId;
      submitClassInfo(this.formClass).then(response => {
        this.dialogVisibleClass = false;
        this.$message.success("上传成功");
      })
    },
    // 打开班级附件弹框
    uploadClassInfo() {
      getInformationClass(this.classId).then(response => {
        this.formClass = response.data;
        this.dialogVisibleClass = true;
        this.title = "上传班级附件";
      });
    },
    // 打开提交考试结论表
    handleSubmit(row) {
      this.stuId = row.id;
      this.reset();
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.dialogVisible = true;
        this.title = "输入学员培训结论";
      });
    },
    // 提交考试结论
    submitExamConclusion() {
      this.dialogVisible = false;
      submitConclusion(this.form).then(response => {
        this.$message.success("提交成功");
        this.getList();
      })
    },
    // 录入考生结论
    handleClose(done) {
        done();
      },
    
    /** 查询学员信息列表 */
    getList() {
      var id = getUrlParam("classId");
      this.classId = id;
      this.loading = true;
      listInformation(id).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNumber: null,
        name: null,
        idCard: null,
        workplace: null,
        phone: null,
        urlPersonApplication: null,
        urlHealthCertificate: null,
        urlDegreeCertificate: null,
        urlIdCopy: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        isDeleted: null,
        extend: null,
        trainClusion: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.classId = this.classId;
        
        if (valid) {
          if (this.form.id != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员信息编号为"' + ids + '"的数据项？').then(function() {
        return delInformation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/information/export', {
        ...this.queryParams
      }, `information_${new Date().getTime()}.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
